﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    /// <summary>
    /// Error handling.
    /// </summary>
    internal class Error
    {

        #region static method DumpError

        /// <summary>
        /// Writes error to error log file.
        /// </summary>
        /// <param name="x"></param>
        public static void DumpError(Exception x)
        {
            DumpError("", x, new StackTrace());
        }

        /// <summary>
        /// Writes error to error log file.
        /// </summary>
        /// <param name="virtualServer">Virtual server name.</param>
        /// <param name="x"></param>
        public static void DumpError(string virtualServer, Exception x)
        {
            DumpError("", x, new StackTrace());
        }

        /// <summary>
        /// Writes error to error log file.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="stackTrace"></param>
        public static void DumpError(Exception x, StackTrace stackTrace)
        {
            DumpError("", x, stackTrace);
        }

        /// <summary>
        /// Writes error to error log file.
        /// </summary>
        /// <param name="virtualServer">Virtual server name.</param>
        /// <param name="x"></param>
        /// <param name="stackTrace"></param>
        public static void DumpError(string virtualServer, Exception x, StackTrace stackTrace)
        {
            try
            {
                string source = stackTrace.GetFrame(0).GetMethod().DeclaringType.FullName + "." + stackTrace.GetFrame(0).GetMethod().Name + "()";

                string errorText = x.Message + "\r\n";
                errorText += "//------------- function: " + source + "  " + DateTime.Now.ToString() + "------------//\r\n";
                errorText += x.Source + " : " + x.Message + "\r\n";
                errorText += x.StackTrace;

                if (x is System.Data.SqlClient.SqlException)
                {
                    System.Data.SqlClient.SqlException sX = (System.Data.SqlClient.SqlException)x;
                    errorText += "\r\n\r\nSql errors:\r\n";

                    foreach (System.Data.SqlClient.SqlError sErr in sX.Errors)
                    {
                        errorText += "\n";
                        errorText += "Procedure: '" + sErr.Procedure + "'  line: " + sErr.LineNumber.ToString() + "  error: " + sErr.Number.ToString() + "\r\n";
                        errorText += "Message: " + sErr.Message + "\r\n";
                    }
                }

                if (x.InnerException != null)
                {
                    errorText += "\r\n\r\n//------------- Innner Exception ----------\r\n" + x.Message + "\r\n";
                    errorText += x.InnerException.ToString();
                }

                DumpError(virtualServer, errorText);
            }
            catch
            {
            }
        }

        /// <summary>
        /// Writes error to error log file.
        /// </summary> 
        /// <param name="virtualServer">Virtual server name.</param>
        /// <param name="errorText">Error text to dump.</param>
        public static void DumpError(string virtualServer, string errorText)
        {
            try
            {
                MyLog.writeErrorToLog(errorText);
            }
            catch
            {
            }
        }

        #endregion
    }
}
